import basePageOptions from '../../mixins/gh-page-base.js';
import mixDecorator from '../../mixins/gh-page-mixins.js';
import regeneratorRuntime from '../../vendor/regenerator-runtime/runtime.js';

Page(mixDecorator(basePageOptions, {
  data: {
    ads: [],
    noticeList: [],
    activeList: [],
    productList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.getSystemInfo({
      success: function(res) {
        console.log(res);
      },
    })
    this.loadAds();
    this.loadNotices();
    this.loadActives();
    this.loadHotProducts();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  /**
   * 事件处理函数
   */
  bindViewTap(e) {
    const item = e.currentTarget.dataset.item;
    let url = '../resource/resource';

    if (typeof item === 'object') {
      this.gh.navigateTo({
        url
      });
      return;
    }

    switch (item) {
      case 1:
        url = '../customer/customer';
        break;
      case 2:
        url = '../income/income';
        break;
      case 3:
        url = '../article/article';
        break;
      case 4:
        url = '../video/video';
        break;
      case 5:
        url = '../message/message';
        break;
      case 6:
        url = '../activity/activity';
        break;
      case 7:
        url = '../product/product';
        break;
    }
    this.gh.navigateTo({
      url
    });
  },
  /**
   * 获取广告
   */
  async loadAds() {
    let res = await this.gh.fetch({
      key: 'adList',
      data: {
        jsonStr: {
          MessageTypeId: 7,
          size: 6
        }
      }
    });
    if (!res || res.code != 1 || !res.data) {
      return;
    }
    for (let item of res.data) {
      item.Thumb = this.gh.utils.filter.absolutePath(item.Thumb);
      item.UrlString = this.gh.utils.filter.absolutePath(item.UrlString);
    }
    this.setData({
      ads: res.data
    });
  },
  /**
   * 获取通知
   */
  async loadNotices() {
    let res = await this.gh.fetch({
      key: 'noticeList',
      data: {
        jsonStr: JSON.stringify({
          MessageTypeId_query: 12,
          pageSize: 4,
          pageIndex: 1
        })
      }
    });

    if (!res || res.code != 1 || !res.data.rows) {
      return;
    }
    for (let item of res.data.rows) {
      item.Thumb = this.gh.utils.filter.absolutePath(item.Thumb);
      item.UrlString = this.gh.utils.filter.absolutePath(item.UrlString);
    }
    this.setData({
      noticeList: res.data.rows
    });
  },
  /**
   * 获取活动
   */
  async loadActives() {
    let res = await this.gh.fetch({
      key: 'activeList',
      data: {
        jsonStr: {
          MessageTypeId: 11,
          size: 10
        }
      }
    });

    if (!res || res.code != 1 || !res.data) {
      return;
    }

    for (let item of res.data) {
      item.Thumb = this.gh.utils.filter.absolutePath(item.Thumb);
      item.UrlString = this.gh.utils.filter.absolutePath(item.UrlString);
    }
    this.setData({
      activeList: res.data
    });
  },
  /**
   * 获取推荐产品
   */
  async loadHotProducts() {
    let res = await this.gh.fetch({
      key: 'hotProductList',
      data: {
        jsonStr: JSON.stringify({
          MessageTypeId: 5,
          size: 50
        })
      }
    });

    if (!res || res.code != 1) {
      return;
    }
    for (let item of res.data) {
      item.Thumb = this.gh.utils.filter.absolutePath(item.Thumb);
      item.UrlString = this.gh.utils.filter.absolutePath(item.UrlString);
      item.dynamicJson = [{
          id: 1,
          label: "办理周期",
          text: "8-12个月"
        },
        {
          id: 2,
          label: "办理周期",
          text: "8-12个月"
        }, {
          id: 3,
          label: "办理周期",
          text: "8-12个月"
        }, {
          id: 4,
          label: "办理周期",
          text: "8-12个月"
        }
      ];
    }
    this.setData({
      productList: res.data
    });
  }
}));